﻿using HardcoreGame.Common.Abstract;
using HardcoreGame.Common.Cores.Seasons;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.GameContent;
using Terraria.GameContent.Drawing;
using Terraria.ModLoader;
namespace HardcoreGame.Common.Systems.SeasonReplace
{
    public class TileTexture : ModSystem
    {
        public override void Load()
        {
            On_TileDrawing.GetTileDrawTexture_Tile_int_int += On_TileDrawing_GetTileDrawTexture_Tile_int_intAutumn;
            On_TileDrawing.GetTileDrawTexture_Tile_int_int += On_TileDrawing_GetTileDrawTexture_Tile_int_intWinter;
        }
        private Texture2D On_TileDrawing_GetTileDrawTexture_Tile_int_intWinter(On_TileDrawing.orig_GetTileDrawTexture_Tile_int_int orig, TileDrawing self, Tile tile, int tileX, int tileY)
        {
            SeasonSystem seasonSystem = ModContent.GetInstance<SeasonSystem>();
            Season currentSeason = seasonSystem.currentSeason;
            int currentDay = seasonSystem.dayCounter;
            Texture2D result = TextureAssets.Tile[tile.TileType].Value;
            if (currentSeason is Winter)
            {
                if (tile.TileType == 488)
                {
                    result = ModContent.Request<Texture2D>("HardcoreGame/Assets/Textures/Tiles/Season/Winter/Tiles_488").Value;
                }
                if (tile.TileType == 2)
                {
                    result = ModContent.Request<Texture2D>("HardcoreGame/Assets/Textures/Tiles/Season/Winter/Tiles_2").Value;
                }
            }
            return result;
        }
        private Texture2D On_TileDrawing_GetTileDrawTexture_Tile_int_intAutumn(On_TileDrawing.orig_GetTileDrawTexture_Tile_int_int orig, TileDrawing self, Tile tile, int tileX, int tileY)
        {
            SeasonSystem seasonSystem = ModContent.GetInstance<SeasonSystem>();
            Season currentSeason = seasonSystem.currentSeason;
            int currentDay = seasonSystem.dayCounter;
            Texture2D result = TextureAssets.Tile[tile.TileType].Value;
            if (currentSeason is Autumn)
            {
                if (currentDay <= 8)
                {
                    if (tile.TileType == 191)
                    {
                        result = ModContent.Request<Texture2D>("HardcoreGame/Assets/Textures/Tiles/Season/Autumn/Tiles_191_0").Value;
                    }
                    if (tile.TileType == 192)
                    {
                        result = ModContent.Request<Texture2D>("HardcoreGame/Assets/Textures/Tiles/Season/Autumn/Tiles_192_0").Value;
                    }
                }
                else
                {
                    if (tile.TileType == 191)
                    {
                        result = ModContent.Request<Texture2D>("HardcoreGame/Assets/Textures/Tiles/Season/Autumn/Tiles_191").Value;
                    }
                    if (tile.TileType == 192)
                    {
                        result = ModContent.Request<Texture2D>("HardcoreGame/Assets/Textures/Tiles/Season/Autumn/Tiles_192").Value;
                    }
                }
                if (tile.TileType == 353)
                {
                    result = ModContent.Request<Texture2D>("HardcoreGame/Assets/Textures/Tiles/Season/Autumn/Tiles_353").Value;
                }
                if (tile.TileType == 488)
                {
                    result = ModContent.Request<Texture2D>("HardcoreGame/Assets/Textures/Tiles/Season/Autumn/Tiles_488").Value;
                }
                if (tile.TileType == 382)
                {
                    result = ModContent.Request<Texture2D>("HardcoreGame/Assets/Textures/Tiles/Season/Autumn/Tiles_382").Value;
                }
                if (tile.TileType == 518)
                {
                    result = ModContent.Request<Texture2D>("HardcoreGame/Assets/Textures/Tiles/Season/Autumn/Tiles_518").Value;
                }
                if (tile.TileType == 519)
                {
                    result = ModContent.Request<Texture2D>("HardcoreGame/Assets/Textures/Tiles/Season/Autumn/Tiles_519").Value;
                }
            }
            return result;
        }
        public override void Unload()
        {
            On_TileDrawing.GetTileDrawTexture_Tile_int_int -= On_TileDrawing_GetTileDrawTexture_Tile_int_intAutumn;
            On_TileDrawing.GetTileDrawTexture_Tile_int_int -= On_TileDrawing_GetTileDrawTexture_Tile_int_intWinter;
        }
    }
}
